package org.emet.help;

import java.text.MessageFormat;
import java.util.Iterator;

import org.emet.dao.DataDicDao;
import org.emet.dao.DataItemDao;
import org.emet.jee.console.entity.sys.DataDic;
import org.emet.jee.console.entity.sys.DataItem;
import org.emet.jee.core.utils.spring.SpringContextHolder;

public class CommonUtils {

	public static String getAddress(String city, String county, String town) {
		DataDicDao dao = SpringContextHolder.getBean("dataDicDao");
		DataDic citys = dao.get(7L);
		String cityStr = getDisplayTextByKey(citys, city);
		DataDic countys = dao.get(8L);
		String countyStr = getDisplayTextByKey(countys, county);
		DataDic towns = dao.get(9L);
		String townStr = getDisplayTextByKey(towns, town);

		return MessageFormat.format("{} {} {}", cityStr, countyStr, townStr);
	}

	private static String getDisplayTextByKey(DataDic citys, String key) {
		Iterator<DataItem> it = citys.getItems().iterator();
		while (it.hasNext()) {
			DataItem item = it.next();
			if (item.getKey().intern() == key.intern())
				return item.getValue();
		}
		return null;
	}

	public static DataItem getDataItem(Long id) {
		DataItemDao dao = SpringContextHolder.getBean("dataItemDao");
		return dao.get(id);
	}

	public static String getDataItemDisplayText(Long dataDicId, String key) {
		DataDicDao dao = SpringContextHolder.getBean("dataDicDao");
		DataDic dataDic = dao.get(dataDicId);
		return getDisplayTextByKey(dataDic, key);
	}
}
